import json


arr = [];
with open("/Users/wangshuguan/Desktop/信息化和信息系统.csv") as f:
    for x in f.readlines():
        arr.append(x.split(","))


size = (len(arr),len(arr[0]))
print(size)
def getChildren(x,y):
    if arr[x][y] != "":
        if y+1 == size[1]:
            return []
        children = [];
        for x1 in range(x,size[0]):
            if arr[x1][y] == "" or x1 == x:
                # 找到孩子的行
                if arr[x1][y+1] != "" and arr[x1][y+1] != "\n":
                    name = arr[x1][y+1]
                    children.append({"name":name.replace("\n",""),"children":getChildren(x1,y+1)})
                    # if arr[x1][y+1] == "细化用例描述":
                    #     print({"name":arr[x1][y+1].replace("\n",""),"children":getChildren(x1,y+1)})
            else:
                # 碰到兄弟的行了
                return children
        # 碰到兄弟的行了
        return children
    return []
obj = {"name":"root","children":getChildren(0,0)};
# print(json.dumps(obj))

targetArr = [];
def getAllChildren(o):
    targetArr.append(o);
    for x in o["children"]:
        _x = x;
        _x["name"] = o["name"]+">"+x["name"]
        getAllChildren(x)
getAllChildren(obj);

targetCSV = [];
for x in targetArr:
    if len(x["children"]) > 0:
        # if ";".join([o["name"] for o in x["children"]]).find("、") > -1:
        #     print(x["name"],";".join([o["name"] for o in x["children"]]))
        name = x["name"]+"包含哪些内容？"
        strings = [o["name"].split(">")[-1] for o in x["children"]]
        _strings = [f"{i+1}:{strings[i]}" for i in range(0,len(strings))]
        value = ";;;".join(_strings)
        targetCSV.append(",".join([name,value]))

with open("/Users/wangshuguan/Desktop/export.csv",'w+') as f:
    f.write("\n".join(targetCSV))
